package pers.chy.chyappointmentplatform.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.chy.chyappointmentplatform.Mapper.HisMapper.Yjyy_PdjhMapper;
import pers.chy.chyappointmentplatform.Mapper.HisMapper.Yjyy_ZsxxMapper;
import pers.chy.chyappointmentplatform.Model.Yjyy_PdjhDTO;
import pers.chy.chyappointmentplatform.Model.Yjyy_ZsxxExtDTO;
import pers.chy.chyappointmentplatform.Utils.ChyException;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 叫号服务类
 */
@Service
public class CallNumberService {

    @Autowired
    private Yjyy_PdjhMapper yjyy_PdjhMapper;
    @Autowired
    private Yjyy_ZsxxMapper yjyy_zsxxMapper;

    /**
     * 叫号
     *
     * @param params
     * @param jhrygh
     * @param jhryxm
     * @return
     * @throws Exception
     */
    @Transactional
    public Yjyy_PdjhDTO doCallNext(Map params, String jhrygh, String jhryxm) throws Exception {

        try {
            Date now = new Date();
            // 参数解析
            long dlxh = Long.parseLong(params.get("dlxh").toString());
            String jhzs = params.get("jhzs").toString();
            int LEDckh = Integer.parseInt(params.get("LEDckh").toString());


            // 获取下一个未叫号的病人
            while (true) {
                Yjyy_PdjhDTO nextOne = yjyy_PdjhMapper.selectCallNext(dlxh);
                if (nextOne == null) {
                    throw new ChyException("目前没有病人!");
                }

                // 叫号（乐观锁）
                nextOne.setJhzs(jhzs);
                nextOne.setJhsj(now);
                nextOne.setJhrygh(jhrygh);
                nextOne.setJhryxm(jhryxm);
                nextOne.setLEDckh(LEDckh);
                int row = yjyy_PdjhMapper.updateForCallNext(nextOne);
                if (row == 0) continue;

                // 如果需要LED上屏，则每次叫号时都更新此窗口的其他病人为'过号'状态
                if (LEDckh > 0) {
                    yjyy_PdjhMapper.updateForCallPass(nextOne); // 更新此窗口的其他所有病人
                }

                return nextOne;
            }
        } catch (Exception ex) {
            throw ex;
        }
    }


    /**
     * 获取诊室信息
     *
     * @return
     */
    public List<Yjyy_ZsxxExtDTO> getZsxx(long dlxh) {
        try {
            List<Yjyy_ZsxxExtDTO> ret = yjyy_zsxxMapper.selectByDlxh(dlxh);
            return ret;
        } catch (Exception ex) {
            throw ex;
        }
    }

    /**
     * 获取叫号数据（最新数据）
     *
     * @return
     */
    public Map<String, List<Yjyy_PdjhDTO>> getLatestData(long dlxh) {

        try {
            // 未叫号的：取号时间早的排前面
            Map condition = new HashMap();
            condition.put("rownum", 30); //返回最多30条记录
            condition.put("dlxh", dlxh);
            condition.put("jhztBegin", 0);
            condition.put("jhztEnd", 0);
            condition.put("yyrq", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            condition.put("orderByPdhmASC", true);
            List<Yjyy_PdjhDTO> wjhList = yjyy_PdjhMapper.selectByConditionAndRownum(condition);

            // 已叫号的：叫号时间晚的排前面
            condition.put("jhztBegin", 1);
            condition.put("jhztEnd", 2);
            condition.remove("orderByPdhmASC");
            condition.put("orderByJhsjAndPdhmDESC", true);
            List<Yjyy_PdjhDTO> yjhList = yjyy_PdjhMapper.selectByConditionAndRownum(condition);

            // TO DO：已过号的..

            // 整合
            Map ret = new HashMap();
            ret.put("wjh", wjhList);
            ret.put("yjh", yjhList);
            return ret;
        } catch (Exception ex) {
            throw ex;
        }
    }

    /**
     * 重叫
     *
     * @return
     */
    public void reCall(Map params) {
        try {
            long id = Long.parseLong(params.get("id").toString()) ;
            Yjyy_PdjhDTO yjyy_pdjhDTO = new Yjyy_PdjhDTO();
            yjyy_pdjhDTO.setId(id);
            yjyy_PdjhMapper.updateForReCall(yjyy_pdjhDTO);
        } catch (Exception ex) {
            throw ex;
        }
    }

}
